package org.example.servlet;


import org.example.dao.ProductDAOImpl;
import org.example.dao.ProductDao1;
import org.example.model.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/activity")
public class ActivityServlet extends HttpServlet {
    private ProductDao1 productDAO = new ProductDAOImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        try {
            // 获取秒杀商品(按折扣排序,取前4个)
            List<Product> flashSaleProducts = productDAO.findProductsByDiscount(4);
            request.setAttribute("flashSaleProducts", flashSaleProducts);
            
            // 获取新品(按上架时间排序,取前4个)
            List<Product> newProducts = productDAO.findNewProducts(4);
            request.setAttribute("newProducts", newProducts);
            
            // 获取热卖商品(按销量排序,取前4个)
            List<Product> hotProducts = productDAO.findHotProducts(4);
            request.setAttribute("hotProducts", hotProducts);
            
            request.getRequestDispatcher("/activity.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
} 